package com.efast.cafe.portal.bean.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.efast.cafe.framework.util.QueryStringUtil;
import com.efast.cafe.framework.util.SqlConstants.FieldType;
import com.efast.cafe.framework.util.SqlConstants.OpType;
import com.alibaba.druid.proxy.jdbc.JdbcParameter.TYPE;
import com.sun.org.apache.bcel.internal.generic.NEW;
@SuppressWarnings("all")
/**
 * 处理各类数据类型的抽象父类
 * @author Administrator
 *
 */
public abstract class AbstractFieldType extends CommonTableConditionBean {
	/**
	 *
	 */
	private static final long serialVersionUID = 8820161034216632688L;
//	public static Map<String ,Object> parameters = new HashMap<>();
//	public static Integer index = 0;

	/**
	 * 处理特殊数据类型
	 * @param t
	 * @return
	 */
	public abstract <T extends CommonTableConditionBean> T getHandledObject(QueryParameterBean p,T t);//一些特殊的数据类型 用combineSql 拼接

	/**
	 * 处理whereCondition 拼接sql
	 * @param op
	 * @param isDefaultValue
	 * @return
	 */
	public static <T extends CommonTableConditionBean> CommonTableConditionBean handelCondition(QueryParameterBean p,T op,Boolean isDefaultValue) {
		try {
			StringBuffer buffer = new StringBuffer();
			CommonTableConditionBean bean = new CommonTableConditionBean();
			String value = op.getValue();
			if (QueryStringUtil.isEmpty(value) && !(OpType.is.equals(op.getOp()) || OpType.is_not.equals(op.getOp()))) {
				if (isDefaultValue) {
					op.setValue(QueryStringUtil.getSqlDefaultValue(op.getKey()));//如果是preview 将将默认值设置为#{key}
					buffer.append(op.getKey()).append(OpType.getOp(op.getOp())).append(op.getValue());
				} else {
					op.setBindSql("1 = 1");
					buffer.append(op.getBindSql());
				}

			} else {
				if (FieldType.map.get(op.getFiledType()) != null) {//需要在sqlConstants里添加数据类型的java Class对象
					FieldType.map.get(op.getFiledType()).newInstance().getHandledObject(p,op);
					buffer.append(QueryStringUtil.getNonNull(op.getCombineSql()));//获得combineSql
				} else {
					String key = op.getKey();
					handelCondition(p,op);//一些简单数据类型在这里维护
					buffer.append(contactSql(p,op,key));
				}

			}
			op.setOpType(OpType.getOp(op.getOp()));
			bean.setBindSql(buffer.toString());
			return bean;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 处理简单数据类型
	 * @param op
	 * @return
	 */
	public static <T extends CommonTableConditionBean> T handelCondition(QueryParameterBean p,T op) {
		String key = op.getKey();
		if (FieldType.Boolean.equals(op.getFiledType())) {
			op.setOpType(OpType.getOp(OpType.eq));
			op.setRealValue(Integer.parseInt(op.getValue()));
		} else if (FieldType.Text.equals(op.getFiledType()) || FieldType.SigleText.equals(op.getFiledType())) {
			if (OpType.Like.equals(op.getOp())) {
				op.setRealValue(QueryStringUtil.like(op.getValue().toLowerCase()));
			} else {
				op.setRealValue(op.getValue());
			}
			op.setOpType(OpType.getOp(op.getOp()));
		}
		op.setValue(QueryStringUtil.getSqlDefaultValue(p.getIndex().toString()));
		return op;

	}
	/**
	 * 获得bindSql的通用方法
	 * @param op
	 * @return
	 */
	public static <T extends CommonTableConditionBean> String contactSql(QueryParameterBean p,T op,String oldKey) {
		String value = QueryStringUtil.getNonNull(op.getValue());
		String key = QueryStringUtil.getNonNull(op.getKey());
		Object realValue = op.getRealValue();
		String jdbcType = FieldType.JdbcMap.get(op.getFiledType());
		if (!OpType.is.equals(op.getOp()) && !OpType.is_not.equals(op.getOp())) {
			//parameters.put(index.toString(), realValue);
			p.getParameters().put(p.getIndex().toString(), realValue);
			System.out.println(realValue);
		}
		StringBuffer buffer = new StringBuffer();
		if (OpType.Like.equals(op.getOp())) {
			buffer.append("LOWER("+key+")");
		} else {
			buffer.append(key);
		}
		buffer.append(QueryStringUtil.getNonNull(op.getOpType()));
		if (OpType.is.equals(op.getOp()) || OpType.is_not.equals(op.getOp())) {
			buffer.append(" NULL ");
		} else {
			buffer.append(value);
			//index++;
			p.setIndex(p.getIndex()+1);
		}
		System.out.println(buffer.toString());

		return buffer.toString();
	}

	public static <T extends CommonTableConditionBean> String covertSql(QueryParameterBean p,String key,String value,Integer op,Integer fieldType) {
		CommonTableConditionBean bean = new CommonTableConditionBean();
		bean.setFiledType(fieldType);
		bean.setKey(key);
		bean.setOp(op);
		bean.setValue(value);
		return handelCondition(p,bean,false).getBindSql();
	}

	public static void main(String[] args) {
		Integer a1 = Integer.valueOf(60);  //danielinbiti
		Integer b1 = 60;
		System.out.println("1:="+(a1.equals(b1)));
		JdbcTemplate template = new JdbcTemplate();
		SqlRowSet set = template.queryForRowSet("");
		set.getMetaData().getColumnTypeName(1);

		Integer a2 = 60;
		Integer b2 = 60;
		System.out.println("2:="+(a2.equals(b2)));


		Integer a3 = new Integer(60);
		Integer b3 = 60;
		System.out.println("3:="+(a3.equals(b3)));

		Integer a4 = 129;
		Integer b4 = 129;
		System.out.println("4:="+(a4.equals(b4)));
	}
}
